import {
  ROLES_ACTION,
  SET_ROLES,
  GET_ROLES,
  GET_PERMI,
  SET_PERMI,
  PERMI_ACTION,
  STORAGE_ROLES,
  STORAGE_PERMI
} from '@/utils/Const';
import { Commit } from 'vuex';
import Storage from '@/utils/Storage';

interface State {
  value: ValueProps
}
interface ValueProps {
  roles: string[],
  permi: string[],
}
export default {
  namespaced: true,
  state: {
    value: {
      roles: Storage.get(STORAGE_ROLES, 1) || [],
      permi: Storage.get(STORAGE_PERMI, 1) || [],
    }
  },
  getters: {
    [GET_ROLES](state: State) {
      return state.value.roles
    },
    [GET_PERMI](state: State) {
      return state.value.permi
    }
  },
  mutations: {
    [SET_ROLES](state: State, payload): void {
      state.value.roles = payload;
      Storage.set(STORAGE_ROLES, payload, 1);
    },
    [SET_PERMI](state: State, payload): void {
      state.value.permi = payload;
      Storage.set(SET_PERMI, payload, 1);
    }
  },
  actions: {
    [ROLES_ACTION](context: { commit: Commit }, paylod) {
      context.commit(SET_ROLES, paylod);
    },
    [PERMI_ACTION](context: { commit: Commit }, paylod) {
      context.commit(SET_PERMI, paylod);
    }
  }
}